import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/mine_business/chat/widget/bubble.dart';
import 'package:tiktokadult/hj_page/mine_business/chat/widget/chat_logic.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';

import '../../../tools_base/loading/loading_center_widget.dart';

// 私信
class MyChatPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChatLogic>(
      init: ChatLogic(),
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              controller.userName.isNotEmpty ? controller.userName.value : "在线客服",
              style: TextStyle(
                color: Color(0xE5FFFFFF),
                fontSize: 18,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          body: () {
            return Column(
              children: [
                Expanded(
                  child: Padding(
                    padding: EdgeInsets.only(
                      top: 14.h,
                      left: 18.w,
                      right: 18.w,
                    ),
                    child: Stack(
                      children: [
                        () {
                          if (controller.isLoading) return LoadingCenterWidget();
                          if (controller.list.isEmpty) return CErrorWidget();
                          return Align(
                            alignment: Alignment.bottomRight,
                            child: pullYsRefresh(
                              onInit: (ctr) => controller.refreshController = ctr,
                              // onRefresh: (_) => controller.onLoadingMore(),
                              onLoading: (_) {
                                print("onLoading");
                                controller.onLoadingMore();
                              },
                              enablePullUp: controller.haveMoreData,
                              enablePullDown: false,
                              noDataText: '',
                              child: ListView.separated(
                                shrinkWrap: true,
                                reverse: true,
                                padding: EdgeInsets.symmetric(vertical: 14.h),
                                separatorBuilder: (_, __) => const SizedBox(
                                  height: 12,
                                ),
                                controller: controller.scrollController,
                                itemCount: controller.list.length,
                                itemBuilder: (context, index) {
                                  return Bubble(
                                    chat: controller.list[index],
                                  );
                                },
                              ),
                            ),
                          );
                        }(),
                      ],
                    ),
                  ),
                ),
                const BottomInputField(),
              ],
            );
          }(),
        );
      },
    );
  }
}
